﻿using System;
using System.Collections;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace DrawMap
{
    public partial class PalmBeach : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            // setup MapViewer control - these can be set here or in the control
            // properties
            ViewMapControl.MapUrl = "PalmBeach.aspx";
            ViewMapControl.MapKey = "key";
            ViewMapControl.MapField = "field";
            ViewMapControl.MapUnits = "feet";
            ViewMapControl.ViewPortSize = new Size(400, 400);
            ViewMapControl.EnvelopeBoundary = 1.5F;
            ViewMapControl.minimumEnvelopSize = 1600;
            ViewMapControl.DisplaySize = new Size(800, 800);

            // create our layers and describe how to draw them
            LoadMap();

            // default selection layer
            ViewMapControl.SelectionLayerName = "parcels";

            if (IsPostBack)
                return;

            // handle the image click - a click on the image is a link to this page
            if (null != Request["layer"])
            {
                ViewMapControl.Image_Click();
                apn.Text = Request["key"];
            }
        }

        protected void doit_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(apn.Text))
            {
                ViewMapControl.SelectionLayerName = "parcels";

                // set values
                ViewMapControl.SelectionValues.Add(new Parameter("PCN", DbType.String, apn.Text));
                LoadMap();

                Situs.Text = "";
                Street.Text = "";
                Type.Text = "";
                Community.Text = "";
            }

            // go draw our map
            ViewMapControl.DrawMapByKey();
        }

        protected void doit_Address_Click(object sender, EventArgs e)
        {
            ViewMapControl.SelectionLayerName = "parcels";

            if (!String.IsNullOrEmpty(Situs.Text))
            {
                Situs.Text = Situs.Text.PadLeft(5, '0'); ;
                ViewMapControl.SelectionValues.Add(new Parameter("Street_Num", DbType.String, Situs.Text));
            }

            if (!String.IsNullOrEmpty(Street.Text))
                ViewMapControl.SelectionValues.Add(new Parameter("STREETNAME", DbType.String, Street.Text));

            if (!String.IsNullOrEmpty(Type.Text))
                ViewMapControl.SelectionValues.Add(new Parameter("TYPE", DbType.String, Type.Text));

            if (!String.IsNullOrEmpty(Community.Text))
                ViewMapControl.SelectionValues.Add(new Parameter("MUNI", DbType.String, Community.Text));

            if (0 != ViewMapControl.SelectionValues.Count)
            {
                apn.Text = "";
                LoadMap();
            }

            // go draw our map
            ViewMapControl.DrawMapByKey();
        }

        protected void LoadMap()
        {
            // parcels
            MapViewer.SqlDataSourceLayer parcels = new MapViewer.SqlDataSourceLayer();
            parcels.source = SqlDataSource1;
            parcels.tablename = "parcels";
            parcels.tablegeocolumn = "geom";
            parcels.keycolumn = "PCN";

            // draw thin borders
            parcels.LayerDrawingOptions.LinePen = new Pen(Brushes.Gray, 1);

            // sidebar display values
            parcels.AddDisplayColumn("PCN", "Parcel No.");
            parcels.AddDisplayColumn("Street_Num", "Number");
            parcels.AddDisplayColumn("Pre_Dir", "Prefix");
            parcels.AddDisplayColumn("STREETName", "Street");
            parcels.AddDisplayColumn("Street_SUF", "Type");
            parcels.AddDisplayColumn("Muni", "Community");
            parcels.AddDisplayColumn("ACRES", "Acres");

            ViewMapControl.Add("parcels", parcels);

            // roads
            MapViewer.SqlDataSourceLayer roads = new MapViewer.SqlDataSourceLayer();
            roads.source = SqlDataSource2;
            roads.tablename = "Centerline";
            roads.tablegeocolumn = "geom";
            roads.keycolumn = "Name";

            // we want to display the road names
            roads.displaykey = true;
            roads.condensekey = false;
            roads.AddDisplayColumn("Prefix", "");
            roads.AddDisplayColumn("Name", "");
            roads.AddDisplayColumn("Type", "");
            roads.AddDisplayColumn("Suffix", "");
            roads.LayerDrawingOptions.DisplayFont = new Font("Arial", 6);
            roads.LayerDrawingOptions.TextClip = 75;

            // write the road lines as faint, nearly transparent
            roads.LayerDrawingOptions.LinePen = new Pen(Color.FromArgb(88, 0xbb, 0xbb, 0xbb));

            ViewMapControl.Add("Roads", roads);
        }
    }
}
